package com.xls.dormitoryservice.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xls.dormitoryservice.entity.Repair;
import com.xls.dormitoryservice.vo.RepairVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
public interface RepairMapper extends BaseMapper<Repair> {

    @Select("SELECT\n" +
            "\trp.id,\n" +
            "\tr.id AS roomId,\n" +
            "\tr.building,\n" +
            "\t( SELECT name FROM building WHERE id = r.building ) AS buildingDesc,\n" +
            "\tr.floor,\n" +
            "\t( SELECT name FROM building WHERE id = r.floor ) AS floorDesc,\n" +
            "\tr.rno,\n" +
            "\trp.goods_name AS goodsName,\n" +
            "\trp.description,\n" +
            "\trp.apply_time AS applyTime,\n" +
            "\t( CASE rp.status WHEN 0 THEN '申请' WHEN 1 THEN '维修中' ELSE '完成' END) AS status,\n" +
            "\trp.phone,\n" +
            "\trp.repair_time AS repairTime,\n" +
            "\trp.remarks \n" +
            "FROM\n" +
            "\trepair rp LEFT JOIN room r ON rp.room_id = r.id\n" +
            "\t${ew.customSqlSegment} ORDER BY rp.apply_time DESC")
    Page<RepairVo> getRepairVoPage(Page<RepairVo> repairVoPage, @Param(Constants.WRAPPER) QueryWrapper<Repair> repairQueryWrapper);

    @Select("SELECT\n" +
            "\trp.id,\n" +
            "\tr.id AS roomId,\n" +
            "\tr.building,\n" +
            "\t( SELECT name FROM building WHERE id = r.building ) AS buildingDesc,\n" +
            "\tr.floor,\n" +
            "\t( SELECT name FROM building WHERE id = r.floor ) AS floorDesc,\n" +
            "\tr.rno,\n" +
            "\trp.goods_name AS goodsName,\n" +
            "\trp.description,\n" +
            "\trp.apply_time AS applyTime,\n" +
            "\t( CASE rp.status WHEN 0 THEN '申请' WHEN 1 THEN '维修中' ELSE '完成' END) AS status,\n" +
            "\trp.phone,\n" +
            "\trp.repair_time AS repairTime,\n" +
            "\trp.remarks \n" +
            "FROM\n" +
            "\trepair rp LEFT JOIN room r ON rp.room_id = r.id\n" +
            "WHERE rp.id = #{id}\n")
    RepairVo getRepair(String id);
}
